package gui.specialGUIs;

import gui.designComponents.TMTPanel;
import gui.designComponents.TMTScrollPane;
import gui.designComponents.TMTTable;

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import util.Auth;
import app.Controller;
import app.Model;
import definitions.Commands;

/**
 * Panel containing a Table to show some information
 * 
 * @author MK
 * 
 */
public class TablePanel extends TMTPanel {

	private TMTTable table;
	private JScrollPane scrollPane;
	private Model model;
	private Controller controller;

	public TablePanel(Model model, Controller controller, String whatIsShown) {

		this.model = model;
		this.controller = controller;

		setLayout(new BorderLayout());
		init(whatIsShown);
	}

	private void init(String whatIsShown) {

		Vector<Vector<String>> rowData = null;
		Vector<String> columnData = null;

		if (whatIsShown.equals(Commands.SHOW_EVENT_OVERVIEW)) {
			rowData = model.getEventEntries();
			columnData = model.getEventAttributeNames();
		} else if (whatIsShown.equals(Commands.SHOW_MEMBER_OVERVIEW)) {
			rowData = model.getMemberEntries();
			columnData = model.getMemberAttributeNames();
		} else if (whatIsShown.equals(Commands.SHOW_EVENT_OVERVIEW_NEAR)) {
			rowData = model.getNearEventEntries();
			columnData = model.getEventAttributeNames();
		} else if (whatIsShown.equals(Commands.SHOW_MEMBER_OVERVIEW_HEALTHY)) {
			rowData = model.getHealthyMemberEntries();
			columnData = model.getMemberAttributeNamesShort();
		} else if (whatIsShown
				.equals(Commands.SHOW_MEMBER_OVERVIEW_NOT_HEALTHY)) {
			rowData = model.getNotHealthyMemberEntries();
			columnData = model.getMemberAttributeNamesShort();
		} else if (whatIsShown.equals(Commands.SHOW_PARTICIPANTS)) {

			rowData = model.getParticipantsForEvent();
			columnData = model.getParticipantsAttributeNames();
		}

		TableModel tableModel = new DefaultTableModel(rowData, columnData);
		this.table = new TMTTable(tableModel);

		table.setName(whatIsShown);

		Boolean authorizated = Auth.getAuthorization(null, model);

		if (authorizated || !whatIsShown.equals(Commands.SHOW_PARTICIPANTS)) {
			table.addMouseListener(controller);
		}

		scrollPane = new TMTScrollPane(table);

		add(scrollPane, BorderLayout.CENTER);

	}

	public void update(String whatIsShown) {
		remove(scrollPane);
		init(whatIsShown);
	}

	public TMTTable getTMTTable() {
		return table;
	}
}